 package com.rgz.controller.weixin;



import com.alibaba.fastjson.JSONObject;

import com.rgz.utils.HttpClientUtil;
import com.rgz.utils.MessageUtil;
import com.rgz.utils.SignUtil;



import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.*;



@RestController
public class TestController {
	private static final String REQESTURL = "http://www.tuling123.com/openapi/api?key=6c2cfaf7a7f088e843b550b0c5b89c26&info=";
   
	@RequestMapping(value="getTest",method = { RequestMethod.POST, RequestMethod.GET }, produces = "text/html;charset=UTF-8")
    public String getTest(HttpServletRequest request,HttpServletResponse response) throws IOException {
    	
        String resXML = "";
        //获取是谁发送的,目的是为了回复消息
        String fromUserName ="";
        String toUserName ="";//代表公众号
        String content = "";//消息内容
        String msgType = "";//代表消息类型
        String resContent = "JAVA 是世界上最好的编程语言";
        String method = request.getMethod();
        if ("get".equalsIgnoreCase(method)){
            //获取腾讯传递过来的一些数据

            String signature = request.getParameter("signature");
            //时间戳
            String timestamp = request.getParameter("timestamp");
            //随机数
            String nonce = request.getParameter("nonce");
            //随机字符串
            String echostr = request.getParameter("echostr");
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                System.out.println("成功");
                return echostr;
            }//这个是回执的随机字符串,如果认证成功,返回给腾讯服务器即可
            return "shibai";
            
            
            
        } else if ("post".equalsIgnoreCase(method)) {
        	
            try {
                Map<String, String> message = MessageUtil.pareXml(request);//将腾讯转发过来的内容解析
                //获取是谁发送的,目的是为了回复消息
                fromUserName = message.get("FromUserName");
                toUserName = message.get("ToUserName");//代表公众号
                content = message.get("Content");//消息内容
                msgType = message.get("MsgType");//代表消息类型
                //此处应该判断消息类型
                if ("text".equalsIgnoreCase(msgType)) {
                	String resultJson = HttpClientUtil.doGet(REQESTURL + content);
        			JSONObject jsonObject = JSONObject.parseObject(resultJson);
        			Integer resultCode = jsonObject.getInteger("code");
        			if(content.equals("世界上最帅的是谁")){
        				resContent="毫无疑问就是你，世界都知道";
        			}else if (resultCode == 100000) {	
        				resContent = jsonObject.getString("text");
        				
        			}else{
                    	resContent="安安还小，不知道你说什么";
        			}
                  
                    
                    resXML="<xml><ToUserName><![CDATA["
                    		+fromUserName+"]]></ToUserName><FromUserName><![CDATA["
                    		+toUserName+"]]></FromUserName><CreateTime>"
                    		+System.currentTimeMillis()+"</CreateTime><MsgType><![CDATA[text]]></MsgType>"
                    		+ "<Content><![CDATA["+resContent+"]]></Content></xml>";
                }
            }catch (Exception e) {
            	//捕捉异常后 将输出固定内容
                resXML="<xml>" +
                        " <ToUserName><![CDATA["+fromUserName+"]]></ToUserName>" +
                        " <FromUserName><![CDATA["+toUserName+"]]></FromUserName>" +
                        " <CreateTime>"+System.currentTimeMillis()+"</CreateTime>" +
                        " <MsgType><![CDATA[text]]></MsgType>" +
                        " <Content><![CDATA[其实这不是异常]]></Content>" +
                        " </xml>";
            }
            return resXML;
			
        
        
        }
        return "shibai";
	}
	
	
}
